#!/usr/bin/perl -wc

package RFCMail::ESMTP::Object::Path;

use strict;
use base qw(RFCMail::ESMTP::Object::Mailbox);
use Exporter qw(import);
use Carp qw(croak);
use RFCMail::ESMTP::Grammar::Base qw(DOMAIN_RE);
use RFCMail::ESMTP::Grammar::Parser qw(split_path
				       split_mailbox);

our @EXPORT_OK = qw(Path);


# Note: function, not method
sub Path {
    my ($lp, $dp, @route) = @_;
    ($lp, $dp) = split_mailbox(split_path($lp))
	if scalar(@_) == 1;
    return (RFCMail::ESMTP::Object::Path->new($lp, $dp, @route));
}


sub new {
    my ($class, $lp, $dp, @route) = @_;
    for (@route) {
	croak("invalid route component '$_'")
	    if not /\A@{[DOMAIN_RE]}\z/o;
    }
    my $self = $class->SUPER::new($lp, $dp);
    push @$self, @route;
    return ($self);
}


sub path {
    my ($self) = @_;
    my ($lp, $dp, @route) = @$self;
    return ('<'.$self->mailbox.'>')
	if not @route;
    return ('<@'.join(',@', @route).':'.$self->mailbox.'>');
}


1;
